<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>33.7. Zend_Pdf module usage example.</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="prev" href="zend.pdf.info.html" title="33.6. Document Info and Metadata.">
<link rel="next" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">33.7. Zend_Pdf module usage example.</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.pdf.info.html">Prev</a> </td>
<th width="60%" align="center">Chapter 33. Zend_Pdf</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.registry.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.pdf.usage"></a>33.7. Zend_Pdf module usage example.</h2></div></div></div>
<p>
    This section provides an example of module usage.
    </p>
<p>
    This example can be found in a <code class="filename">demos/Zend/Pdf/demo.php</code> file.
    </p>
<p>
    There are also <code class="filename">test.pdf</code> file, which can be used with this demo
    for test purposes.
    </p>
<div class="example">
<a name="zend.pdf.usage.example-1"></a><p class="title"><b>Example 33.17. Zend_Pdf module usage demo.</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
/**
 * @package Zend_Pdf
 * @subpackage demo
 */

/** Zend_Pdf */
require_once 'Zend/Pdf.php';

if (!isset($argv[1])) {
    echo "USAGE: php demo.php &lt;pdf_file&gt; [&lt;output_pdf_file&gt;]\n";
    exit;
}

try {
    $pdf = Zend_Pdf::load($argv[1]);
} catch (Zend_Pdf_Exception $e) {
    if ($e-&gt;getMessage() == 'Can not open \'' . $argv[1] . '\' file for reading.') {
        // Create new PDF if file doesn't exist
        $pdf = new Zend_Pdf();

        if (!isset($argv[2])) {
            // force complete file rewriting (instead of updating)
            $argv[2] = $argv[1];
        }
    } else {
        // Throw an exception if it's not the "Can't open file" exception
        throw $e;
    }
}

//------------------------------------------------------------------------------------
// Reverse page order
$pdf-&gt;pages = array_reverse($pdf-&gt;pages);

// Create new Style
$style = new Zend_Pdf_Style();
$style-&gt;setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
$style-&gt;setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
$style-&gt;setLineWidth(3);
$style-&gt;setLineDashingPattern(array(3, 2, 3, 4), 1.6);
$style-&gt;setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);

try {
    // Create new image object
    $stampImage = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/stamp.jpg');
} catch (Zend_Pdf_Exception $e) {
    // Example of operating with image loading exceptions. 
    if ($e-&gt;getMessage() != 'Image extension is not installed.' &amp;&amp;
        $e-&gt;getMessage() != 'JPG support is not configured properly.') {
        throw $e;
    }
    $stampImage = null;
}

// Mark page as modified
foreach ($pdf-&gt;pages as $page){
    $page-&gt;saveGS();
    $page-&gt;setStyle($style);
    $page-&gt;rotate(0, 0, M_PI_2/3);

    $page-&gt;saveGS();
    $page-&gt;clipCircle(550, -10, 50);
    if ($stampImage != null) {
        $page-&gt;drawImage($stampImage, 500, -60, 600, 40);
    }
    $page-&gt;restoreGS();

    $page-&gt;drawText('Modified by Zend Framework!', 150, 0);
    $page-&gt;restoreGS();
}

// Add new page generated by Zend_Pdf object (page is attached to the specified document)
$pdf-&gt;pages[] = ($page1 = $pdf-&gt;newPage('A4'));

// Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
$pdf-&gt;pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));

// Create new font
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

// Apply font and draw text
$page1-&gt;setFont($font, 36);
$page1-&gt;drawText('Helvetica 36 text string', 60, 500);

// Use font object for another page
$page2-&gt;setFont($font, 24);
$page2-&gt;drawText('Helvetica 24 text string', 60, 500);

// Use another font
$page2-&gt;setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN), 32);
$page2-&gt;drawText('Times-Roman 32 text string', 60, 450);

// Draw rectangle
$page2-&gt;setFillColor(new Zend_Pdf_Color_GrayScale(0.8));
$page2-&gt;setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
$page2-&gt;setLineDashingPattern(array(3, 2, 3, 4), 1.6);
$page2-&gt;drawRectangle(60, 400, 400, 350);

// Draw circle
$page2-&gt;setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID);
$page2-&gt;setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0));
$page2-&gt;drawCircle(85, 375, 25);

// Draw sectors
$page2-&gt;drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6);
$page2-&gt;setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0));
$page2-&gt;drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3);
$page2-&gt;setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0));
$page2-&gt;drawCircle(200, 375, 25, -M_PI/6, M_PI/6);

// Draw ellipse
$page2-&gt;setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0));
$page2-&gt;drawEllipse(250, 400, 400, 350);
$page2-&gt;setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0));
$page2-&gt;drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3);
$page2-&gt;setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0));
$page2-&gt;drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);

// Draw and fill polygon
$page2-&gt;setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
$x = array();
$y = array();
for ($count = 0; $count &lt; 8; $count++) {
    $x[] = 140 + 25*cos(3*M_PI_4*$count);
    $y[] = 375 + 25*sin(3*M_PI_4*$count);
}
$page2-&gt;drawPolygon($x, $y,
                    Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                    Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);

// Draw line
$page2-&gt;setLineWidth(0.5);
$page2-&gt;drawLine(60, 375, 400, 375);
//------------------------------------------------------------------------------------

if (isset($argv[2])) {
    $pdf-&gt;save($argv[2]);
} else {
    $pdf-&gt;save($argv[1], true /* update */);
}
        </pre></div>
</div>
<br class="example-break">
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.pdf.info.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.pdf.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.registry.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">33.6. Document Info and Metadata. </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Chapter 34. Zend_Registry</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
